import {
    createSlice,
    createAsyncThunk
} from '@reduxjs/toolkit'

//引入api里的axios请求

import {MovieRequest} from '../../api'


//创建发送请求的action,并暴露

export const getMovieAction  = createAsyncThunk('movie/getMovieAction' , async()=>{
    //拿到请求值
        const re = await MovieRequest()
        console.log(re,'re');
        //拿到re之后返回list
        return re.data.data.list
    })

const movieSlice = createSlice({
    name:'movie',

    //初始值
    initialState:{
        movieList : []
    },

    reducers:{},

    //异步请求不需要写道上面renducers，因为会再生成一次action 

    // extraReducers 说明创建指定额外的 reducer,做为全局性的reducers。 相当于一个钩子,每次触发dispatch,都会调用该方法,重新组合reducers
    extraReducers:{
        [getMovieAction.fulfilled](prev,action){
            console.log('异步成功');
            prev.movieList = action.payload
        },
        [getMovieAction.rejected](){
            console.log('异步失败');
        },
        [getMovieAction.pending](){
            console.log('异步进行中');
        },
    }
})


export default movieSlice.reducer;